package com.iye.puretennis.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
import com.iye.puretennis.R;
import com.iye.puretennis.custom.CustomViewFlipper;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2014/12/11
 * Time: 14:00
 */
public class SwitchPageActivity extends Activity {
    CustomViewFlipper myViewFlipper = null;
    String[] strings = {"第一章", "第二章", "第三章", "第四章", "第五章", "第六章", "第七章",
            "第八章", "第九章"};
    int totalNum = 9;
    ViewElement[] viewElements = new ViewElement[2];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        useMyViewFlipper();
    }

    private void useMyViewFlipper() {
        setContentView(R.layout.switch1);
        myViewFlipper = (CustomViewFlipper) findViewById(R.id.myViewFlipper1);
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View view = layoutInflater.inflate(R.layout.bb, null);
        View view2 = layoutInflater.inflate(R.layout.bb, null);
        viewElements[0] = new ViewElement(view);
        viewElements[1] = new ViewElement(view2);
        myViewFlipper.setMyViewFlipper(totalNum, false, 4, new View[]{view,
                view2}, loadViewData, loadViewData);
    }

    CustomViewFlipper.MyViewFlipperLoadData loadViewData = new CustomViewFlipper.MyViewFlipperLoadData() {
        public void loadData(int index) {
            viewElements[index].textView.setText(strings[myViewFlipper
                    .getCurrentDataIndex()]);
        }

        @Override
        public boolean canLoadData() {
            return true;
        }
    };

    public class ViewElement {
        TextView textView;
        ScrollView sl;

        public ViewElement(View view) {
            this.textView = (TextView) view.findViewById(R.id.tv);
            this.sl = (ScrollView) view.findViewById(R.id.ScrollView1);
            // ScrollView滑动时触发切换事件
            myViewFlipper.setChildTouchListener(this.sl);
        }
    }
}
